package io.renren.modules.peoplemanage.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/upload")
public class UploadController {
    @Value("${upload.uploadPath}")
    private String uploadPath;
    /**
     * ueditor上传图片的方法
     * @param upfile 上传图片的文件
     * @param request 
     * @param response
     * @return
     */
    @RequestMapping(value="/uploadimage",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> uploadNewsImg(@RequestParam(value="upfile") MultipartFile upfile,HttpServletRequest request,HttpServletResponse response) {
    	Date date = new Date();
        String upLoadPath = uploadPath+"\\upload\\file\\"+new SimpleDateFormat("yyyy\\MM\\dd\\").format(date);
        String path = upLoadPath;
        //图片后缀
        String last = upfile.getOriginalFilename().substring(upfile.getOriginalFilename().lastIndexOf("."), upfile.getOriginalFilename().length());
        
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String fileName = uuid+last;
        
        File fileT = new File(path,fileName);
        if(!fileT.exists()){
            fileT.mkdirs();
        }
        Map<String, Object> result = new HashMap<String,Object>();
        try {
            upfile.transferTo(fileT);
        } catch (IllegalStateException e) {
        	e.printStackTrace();
        } catch (IOException e1) {
        	e1.printStackTrace();
        }
        result.put("state", "SUCCESS");
        result.put("url",   "\\image\\upload\\file\\"+new SimpleDateFormat("yyyy\\MM\\dd\\").format(date)+fileName);
        result.put("original", "");
        result.put("type", last);
        result.put("size", upfile.getSize());
        result.put("title", fileName);
        return result;
    }
    
    
    /**
     * ueditor文件上传方法
     * @param upfile
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/uploadfile",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> uploadFile(@RequestParam(value="upfile",required=false) MultipartFile upfile,HttpServletRequest request,HttpServletResponse response) {
        Date date = new Date();
        String upLoadPath = "D:\\JT_IMAGE\\upload\\file\\"+new SimpleDateFormat("yyyy\\MM\\").format(date);
        String path = upLoadPath;
        //附件后缀
        String last = upfile.getOriginalFilename().substring(upfile.getOriginalFilename().lastIndexOf("."), upfile.getOriginalFilename().length());
        
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String fileName = uuid+last;
        
        File fileT = new File(path,fileName);
        if(!fileT.exists()){
            fileT.mkdirs();
        }
        Map<String, Object> result = new HashMap<String,Object>();
        try {
            upfile.transferTo(fileT);
        } catch (IllegalStateException e) {
        } catch (IOException e1) {
        }
        result.put("state", "SUCCESS");
        result.put("url","http:\\\\youer.com\\upload\\file\\"+new SimpleDateFormat("yyyy\\MM\\").format(date)+fileName);
        result.put("original", "");
        result.put("type", last);
        result.put("size", upfile.getSize());
        result.put("title", fileName);
        return result;
    }
}